<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {
      background-color: #eee;
    }

    #app {
      background-color: #fff;
      width: 500px;
      margin: 50px auto;
      box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.5);
      padding: 2em;
    }

    .box {
      padding: 1em;
      border: 1px solid #ccc;
      margin: 1em;
    }
  </style>
</head>

<body>
  <div id="app">
    <com-d></com-d>
    <com-b></com-b>
  </div>
  <!-- 导入js文件 -->
  <script src="https://cdn.bootcss.com/vue/2.6.11/vue.min.js"></script>
  <script>

    Vue.component('com-d', {
      template: `<div>
        <h1 @click="hClick">组件D</h1>
        <com-a></com-a>
        <com-c></com-c>
        </div>`,
      methods: {
        hClick() {

        }
      }
    })

    Vue.component('com-b', {
      template: `<h1>组件B: {{num}}</h1>`,
      created() {
        this.$eventBus.$on('add1', (n) => {
          console.log('我com-b, 收到了add1..., 参数是', n)
          console.log(this, this.num)
          this.num++
        })
      },
      data() {
        return {
          num: 1
        }
      }
    })
    Vue.component('com-c', {
      data() {
        return {
          num: 100
        }
      },
      created() {
        this.$eventBus.$on('add1', () => {
          this.num--
        })
      },
      template: `<div>组件C: {{num}}</div>`

    })

    Vue.component('com-a', {

      template: `<div>组件A<button @click="hClick">点击给b+1,给c-1</button></div>`,
      methods: {
        hClick() {
          this.$eventBus.$emit('add1', 20)
        }
      }
    })

    Vue.prototype.$eventBus = new Vue()
    new Vue({
      el: "#app",
      data: {
        msg: '你好'
      },
      methods: {
      }
    });
  </script>
</body>

</html>